﻿using UnityEngine;
using System.Collections;
using System;
using UnityEngine.SceneManagement;
using Assets.MatchVS;
using LitJson;

public class Lobby : MonoBehaviour {
    DiaLogController listDialog1;
    DiaLogController listDialog2;
    DiaLogController listDialog3;
    Dialog dialog;

    public void onClick(GameObject o) {
        //Debug.Log(this + "(" + this.GetHashCode() + ")" + "create dialog" + listDialog1.dialog.GetHashCode());
        switch(o.name) {
            case "BtnBottomBar1":
                listDialog1.dialog.show();
                if(!listDialog1.isAddListView) {
                    UIUtil.removeAll(listDialog1.listView);
                    for(int i = 0; i < 10; i++) {
                        UIUtil.AddItem("ItemDialogListView", listDialog1.listView, "任务"+i, "在对战场中获得"+(i+1)*10+"次胜利", "", "http://img2.niushe.com/upload/201306/27/16-59-51-28-59983.jpg", i , listDialog1.onItemClick);
                    }
                    listDialog1.isAddListView = true;
                }
                break;
            case "BtnBottomBar2":
                listDialog2.dialog.show();
                if(!listDialog2.isAddListView) {
                    UIUtil.removeAll(listDialog1.listView);
                    for(int i = 0; i < 10; i++) {
                        UIUtil.AddItem("ItemDialogListView", listDialog2.listView, "成就" + i, "在对战场中获得" + (i + 1) * 10 + "次胜利", "", "http://pic40.nipic.com/20140407/14649865_093349377124_2.jpg", i, listDialog2.onItemClick);
                    }
                    listDialog2.isAddListView = true;
                }
                break;
            case "BtnBottomBar3":
                SceneManager.LoadScene("ScenceTopList");
                break;
            case "BtnBottomBar4":
                listDialog3.dialog.show();
                if(!listDialog3.isAddListView) {
                    UIUtil.removeAll(listDialog3.listView);
                    for(int i = 0; i < MatchVS.goodsids.Length; i++) {
                        UIUtil.AddItem("ItemDialogListView", listDialog3.listView, "商品id"+MatchVS.goodsids[i], "商品:"+ MatchVS.goodsNames[i], "", "https://cdn.tinypng.com/images/example-shrunk.png?c02bdf6", i, listDialog3.onItemClick);
                    }
                    listDialog3.isAddListView = true;
                    listDialog3.listView.Reposition();
                }
                break;
            case "BtnBottomBar5":
                SceneManager.LoadScene("ScenceSetting");
                break;
            case "BtnExit":
                SceneManager.LoadScene("ScenceMain");
                break;
            case "BtnGift":
                dialog.show();
                dialog.setText("礼物");
                Http.awardRec(this,(text,err)=> {
                    //{ "data":{ "awardID":"1","dayCount":"0","historyCount":31},"status":0}
                    JsonData data = LitJson.JsonMapper.ToObject(text)["data"];
                    String s = "奖品ID:"+data["awardID"]+"\n\n 今天领取了"+data["dayCount"]+"次\n\n历史领取了"+data["historyCount"]+"次";
                    dialog.setText(s);
                },MatchVS.appkey,MatchVS.token,MatchVS.userID,MatchVS.gameID, MatchVS.awardID);
                break;
            case "BtnSign":
                dialog.show();
                dialog.setText("签到奖励 \n\n 第1天 $30 \n\n 第2天 $100 \n\n 第3天 $150 \n\n 第4天 $200");
                Http.loginSubsidy(this, (text, err) => {
                //www on response->{ "code":7001,"msg":"There is no login-subsidy for this game in 2016-08-04","status":1}
                    JsonData rsp = LitJson.JsonMapper.ToObject(text);
                    if(rsp["status"].ToString().Equals("1")) {
                        dialog.setText("签到失败 \n\n"+rsp["msg"]);
                    } else {
                        JsonData data = LitJson.JsonMapper.ToObject(text)["data"];
                        //{ "code":0,"continuedays":1,"data":[{ "count":100,"day":"01","gameID":102000,
                        //"month":"00","productID":144,"runningdays":3,"subsidytype":6,"type":1,"year":"0000"}],"msg":"success","status":0}
                        String temp="";
                        for(int i = 0; i < data.Count; i++) {
                            temp+="\n"+ rsp["continuedays"]+":奖励"+ data[i]["productID"];
                        }
                        dialog.setText("签到成功 \n\n" + temp);
                    }
                }, MatchVS.appkey,MatchVS.gameID, MatchVS.userID, MatchVS.token);
                break;
            case "BtnFieldMoney":
                SceneManager.LoadScene("ScenceBattle");
                break;
            case "BtnFieldFree":
                SceneManager.LoadScene("ScenceBattle");
                break;
                
        }
    }

	// Use this for initialization
	void Start () {
	    UIUtil.setListener("BtnBottomBar1", onClick);
	    UIUtil.setListener("BtnBottomBar2", onClick);
	    UIUtil.setListener("BtnBottomBar3", onClick);
        UIUtil.setListener("BtnBottomBar4", onClick);
        UIUtil.setListener("BtnBottomBar5", onClick);
        UIUtil.setListener("BtnExit", onClick);
        UIUtil.setListener("BtnGift", onClick);
        UIUtil.setListener("BtnSign", onClick);
        UIUtil.setListener("BtnFieldFree", onClick);
        UIUtil.setListener("BtnFieldMoney", onClick);


        getMyBattleInfo();
        getRoundResult();
        dialog = Dialog.create("Dialog",(GameObject o,bool isok)=> {

        });
        listDialog1 = new DiaLogController("ListDialog", (GameObject o, bool isOk) => {
            if(isOk) {
                Debug.Log("ok");
            } else {
                Debug.Log("cancle");
            }

        });
        listDialog2 = new DiaLogController("ListDialogAchievements", (GameObject o, bool isOk) => {
            if(isOk) {
                Debug.Log("ok");
            } else {
                Debug.Log("cancle");
            }

        });
        listDialog3 = new DiaLogController("ListDialogShop", (GameObject o, bool isOk) => {
            if(isOk) {
                Debug.Log("ok");
            } else {
                Debug.Log("cancle");
            }

        });

    }

    private void getMyBattleInfo() {
        Http.getUserBattleInfo(this, (text, err) => {

            Debug.Log("UserBattleInfo:"+text);

        }, MatchVS.appkey,MatchVS.token,MatchVS.userID,MatchVS.gameID);
        
    }
    private void getRoundResult() {

        Http.getRoundResult(this, (text, err) => {

            Debug.Log("UserBattleInfo:" + text);

        }, MatchVS.appkey, MatchVS.token, MatchVS.userID, MatchVS.gameID, 1);

    }
}
